package com.zmht.beike.biz.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zmht.beike.biz.CognitionBiz;
import com.zmht.beike.core.BaseBizImpl;
import com.zmht.beike.po.TCognition;

public class CognitionBizImpl extends BaseBizImpl implements CognitionBiz {

	public TCognition findById(Serializable id) {
		return (TCognition)this.getCognitionDAO().findById(TCognition.class, id);
	}

	public TCognition findByStudentId(String studentId) {
		String hql = " from TCognition t where t.studentId="+studentId+" order by t.id desc";
		List<TCognition> list = this.getCognitionDAO().findByHQLObj(hql);
		if(list!=null&&list.size()>0){return list.get(0);}
		return null;
	}

	public boolean saveTCognition(TCognition cognition) {
		try{
			this.getCognitionDAO().saveObj(cognition);
		}catch(Exception e){
			return false;
		}
		return true;
	}

	public boolean updateTCognition(TCognition cognition) {
		try{
			this.getCognitionDAO().updateObj(cognition);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	public static void main(String [] args){
		ApplicationContext cxt;
		cxt = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml","spring/spring_dao.xml","spring/spring_beike.xml"});
		CognitionBiz biz = (CognitionBizImpl)cxt.getBean("cognitionBizTag");
		
		
		//保存
		TCognition cognition = new TCognition();
		cognition.setStudentId(3);
		cognition.setMusic("aaa");
		biz.saveTCognition(cognition);
		
		
		/*TCognition cognition = biz.findById(new Integer(1));
		System.out.println(cognition.getStudentId());
		
		cognition.setAppetite("aaaaaaa");
		cognition.setArt("art");
		biz.updateTCognition(cognition);*/
		
	}

	public List<TCognition> findByStudent(String studentId,String startTime,String endTime) {
		String hql = " from TCognition t where t.studentId="+studentId;
		if(!"".equals(startTime)){hql += " and t.ctime>='"+startTime+"' ";}
		if(!"".equals(endTime)){hql += " and t.ctime<='"+endTime+"' ";}
		List<TCognition> list = this.getCognitionDAO().findByHQLObj(hql);
		return list;
	}
}
